package cn.kgc.controller;

import cn.kgc.entity.Dog;
import cn.kgc.mapper.DogMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨泓
 * @version 1.0
 * @date 2023/2/27 - 20:35
 */
@Controller
public class DogController {
    @Resource
    private DogMapper dogMapper;
    @GetMapping("/getByPage")
    public String getByPage(Model model,
                            @RequestParam(value="pageNum",defaultValue = "1")Integer pageNum){
        //current :当前页 1 size:每页的显示数 2
        Page<Dog>page=new Page<>(pageNum,2);
        //返回page对象
        Page<Dog> myUsersPage= dogMapper.selectPage(page,null);
        //获取集合
        List<Dog>records=myUsersPage.getRecords();
        records.forEach(System.out::println);
        System.out.println("总行数:"+myUsersPage.getTotal());
        System.out.println("总页数:"+myUsersPage.getPages());
        System.out.println("当前页:"+myUsersPage.getCurrent());
        System.out.println("每页显示数:"+myUsersPage.getSize());
        model.addAttribute("page",myUsersPage);
        //怎么获取集合
        return "mydog";
    }
    //新增
    @RequestMapping("/add")
    public String add(){
        return "adddog";
    }
    @RequestMapping("/adddog")
    public String adddog(Dog dog){
        int insert=dogMapper.insert(dog);
        return "redirect:getByPage";
    }
    //删除
    @RequestMapping("/deleteDog")
    public String deleteDog(@RequestParam("dogId") Integer dogId){
        Integer integer=dogMapper.deleteById(dogId);
        return "redirect:getByPage";
    }
    //修改
    @RequestMapping("/getDogById")
    public String getDogById(Model model,@RequestParam("dogId")Integer dogId){
        Dog dog=dogMapper.selectById(dogId);
        model.addAttribute("dog",dog);
        return "updatedog";
    }
    @RequestMapping("/updatedog")
    public String updateTelevision(Dog dog){
        dogMapper.updateById(dog);
        return "redirect:getByPage";
    }



}
